﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using Automobile.service.Model;

namespace Automobile.service.DAL
{
    public class MemberCardDal
    {
        string str = "Data Source=.;Initial Catalog=qichefuwu;Integrated Security=True";
        /// <summary>
        /// 会员信息显示
        /// </summary>
        /// <returns></returns>
        public List<MemberCardModel> xian()
        {
            string sql = "select MemberCard_Id,MemberCard_Name,MemberCard_owner,MemberCard_Cnum,MemberCard_CardID,MemberCard_ingeral,MemberCard_sphere,Rank_Name,MemberCard_State from MemberCard";
            SqlDataAdapter apter = new SqlDataAdapter(sql, str);
            DataTable table = new DataTable();
            apter.Fill(table);
            List<MemberCardModel> list = new List<MemberCardModel>();
            foreach (DataRow item in table.Rows)
            {
                MemberCardModel m = new MemberCardModel();
                m.MemberCard_Id = Convert.ToInt32(item["MemberCard_Id"]);
                m.MemberCard_Name = item["MemberCard_Name"].ToString();
                m.MemberCard_owner = item["MemberCard_owner"].ToString();
                m.MemberCard_Cnum = item["MemberCard_Cnum"].ToString();
                m.MemberCard_CardID = item["MemberCard_CardID"].ToString();
                m.MemberCard_ingeral = Convert.ToInt32(item["MemberCard_ingeral"]);
                m.MemberCard_sphere = Convert.ToDecimal(item["MemberCard_sphere"]);
                m.Rank_Name = item["Rank_Name"].ToString();
                m.MemberCard_State = item["MemberCard_State"].ToString();
                list.Add(m);
            }
            return list;
        }
        /// <summary>
        /// 取单个库房信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetMemberCard(string MemberCard_Id)
        {
            string sql = "select  MemberCard_Id,MemberCard_Name,MemberCard_owner,MemberCard_Cnum,MemberCard_CardID,MemberCard_ingeral,MemberCard_sphere,Rank_Name,MemberCard_State from MemberCard where MemberCard_Id=" + MemberCard_Id;

            SqlConnection conn = new SqlConnection(str);
            conn.Open();//打开数据库

            SqlCommand comm = new SqlCommand(sql, conn);
            DataTable table = new DataTable();
            SqlDataAdapter ada = new SqlDataAdapter(comm);
            ada.Fill(table);
            conn.Close();
            return table;
        }
        /// <summary>
        /// 添加会员信息
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public int AddMemberCard(MemberCardModel me)
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            string st = string.Format("insert into MemberCard values('{0}','{1}','{2}','{3}',{4},{5},'{6}','{7}')",me.MemberCard_Id,me.MemberCard_Name,me.MemberCard_owner,me.MemberCard_Cnum,me.MemberCard_CardID,me.MemberCard_ingeral,me.MemberCard_sphere,me.Rank_Name,me.MemberCard_State);
            SqlCommand cmd = new SqlCommand(st, conn);
            int i = cmd.ExecuteNonQuery();
            conn.Close();
            conn.Dispose();
            return i;
        }
        /// <summary>
        ///修改会员信息
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        public int UpdateMemberCard(MemberCardModel mem)
        {
            string sql = "update MemberCard set MemberCard_Name='" + mem.MemberCard_Name + "',MemberCard_owner='" + mem.MemberCard_owner + "',MemberCard_Cnum='" + mem.MemberCard_Cnum + "',MemberCard_CardID='" + mem.MemberCard_CardID + "' MemberCard_ingeral='" + mem.MemberCard_ingeral + "'MemberCard_sphere='" + mem.MemberCard_sphere + "'Rank_Name='" + mem.Rank_Name + "'MemberCard_State='" + mem.MemberCard_State + "' where MemberCard_Id='" + mem.MemberCard_Id + "'";

            SqlConnection conn = new SqlConnection(str);
            conn.Open();//打开数据库

            SqlCommand comm = new SqlCommand(sql, conn);
            //执行命令
            int count = comm.ExecuteNonQuery();
            conn.Close();

            return count;
        }
        /// <summary>
        /// 删除会员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleMemberCard(int MemberCard_Id)
        {
            string sql = "delete from MemberCard where MemberCard_Id=" + MemberCard_Id;
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            int i = comm.ExecuteNonQuery();
            conn.Close();
            return i;
        }
    }
}
